package com.xiaohong.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.user.pojo.User;
import com.xiaohong.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "User接口文档")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /***
     * 分页条件搜索实现User
     * @param user
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询User",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "User对象",required = true) User user, @PathVariable int page, @PathVariable  int size){
        //调用UserService实现分页条件查询User
        Page pageInfo = userService.findPage(user, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现User
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询User",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用UserService实现分页查询User
        Page pageInfo = userService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索User数据
     * @param user
     * @return
     */
    @Operation(summary = "多条件查询User",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<User>> findList(@RequestBody(required = false) @Parameter(name = "User对象",required = true) User user){
        //调用UserService实现条件查询User
        List<User> list = userService.findList(user);
        // 格式化数据
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < list.size(); i++) {
            User user1 = list.get(i);
            user1.setGmtCreateStr(format.format(user1.getGmtCreate()));
            user1.setGmtModifiedStr(format.format(user1.getGmtModified()));
        }

        return new Result<List<User>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除user数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除User",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用UserService实现根据ID删除
        userService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改User数据
     * @param user
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改User",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "User对象",required = true) User user,@PathVariable Integer id){
        // 补充id
        user.setId(id);
        //调用UserService实现修改User
        userService.update(user);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增User数据
     * @param user
     * @return
     */
    @Operation(summary = "添加User",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "User对象",required = true) User user){
        //调用UserService实现添加User
        userService.add(user);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询User数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询User",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<User> findById(@PathVariable Integer id){
        //调用UserService实现根据ID查询User
        User user = userService.findById(id);
        return new Result<User>(true,StatusCode.OK,"查询成功",user);
    }

    /***
     * 查询User全部数据
     * @return
     */
    @Operation(summary = "查询所有User",description = "所需权限：")
    @GetMapping
    public Result<List<User>> findAll(){
        //调用UserService实现查询所有User
        List<User> list = userService.findAll();
        return new Result<List<User>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
